package com.arsn.myf.flow.controller;

import com.arsn.myf.common.util.result.R;
import com.arsn.myf.flow.entity.FlowProcdef;
import com.arsn.myf.flow.service.FlowDefinitionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import static com.arsn.myf.common.util.SteamUtils.inputStreamToByteArray;

/**
 * 流程定义
 * @author zhongrj
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/flow/definition")
@Api(value = "流程定义", tags = "流程定义")
public class FlowDefinitionController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private FlowDefinitionService flowDefinitionService;

    /**
     * 分页
     * @param flow flow 实体
     * @param page page 对象
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入flow")
    public R list(Page<FlowProcdef> page, FlowProcdef flow) {
        return R.data(flowDefinitionService.selectPageFlowProcdefList(page, flow));
    }


    /**
     * 上传部署流程文件
     *
     * @param file   流程文件
     */
    @PostMapping("deployUpload")
    @ApiOperation(value = "上传部署流程文件", notes = "传入文件")
    public R deployUpload(@RequestParam MultipartFile file) throws IOException {
        //文件转换
        String fileName = file.getOriginalFilename();
        byte[] bytes = inputStreamToByteArray(file.getInputStream());
        //部署
        Deployment deployment = repositoryService.
                createDeployment().
                addBytes(fileName, bytes).
                deploy();
        //返回
        return R.status(true);
    }

    /**
     * 删除流程
     * @param ids 需要删除的id
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入主键集合")
    public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        for (String id : list) {
            FlowProcdef procdef = flowDefinitionService.getById(id);
            //删除
            repositoryService.deleteDeployment(procdef.getDeploymentId(),true);
        }
        return R.status(true);
    }

    /**
     * 变更流程状态
     * @param id 流程定义id
     * @param state 状态  1：激活  2：挂起
     */
    @PostMapping("/updateState")
    @ApiOperation(value = "变更流程状态")
    public R updateState(@ApiParam(value = "主键") @RequestParam String id,@RequestParam Integer state) {
        if (state==1){
            repositoryService.activateProcessDefinitionById(id, true, null);
        }
        if (state==2){
            repositoryService.suspendProcessDefinitionById(id, true, null);
        }
        return R.status(true);
    }

    /**
     * 获取流程图
     * @param id 流程定义id
     * @return
     */
    @GetMapping("/getDiagram")
    @ApiOperation(value = "变更流程状态")
    public R getDiagram(@ApiParam(value = "主键") @RequestParam String id) {
        return R.data(flowDefinitionService.getDiagram(id));
    }

}
